﻿/* 
 * 
 * Copyright (c) 2010 Photonic Games

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */


using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline;

namespace GameLevel.ContentPipeline.Leadwerks14
{

    public class Mesh
    {
        public Byte flags;
        public KeyValuePair<string, string>[] keys;
        public Int32 group;
        public Int32 visGroup;
        public Color meshColor;
        public Int32 meshId;
        public Vector3 position;
        public Vector3 rotation;
        public Vector3 scale;
        public MeshReference meshRef;
        

        public Mesh(BinaryReader reader, string[] names, Leadwerks14Content content)
        {
            flags = reader.ReadByte();

            keys = new KeyValuePair<string, string>[reader.ReadInt32()];
            for (int index = 0; index < keys.Length; index++)
            {
                // Entity item keys and values are just strings...
                string key = names[reader.ReadInt32() - 1];
                string value = names[reader.ReadInt32() - 1];

                // Set this entity item
                this.keys[index] = new KeyValuePair<string, string>(key, value);
            }


            group = reader.ReadInt32();
            visGroup = reader.ReadInt32();

            meshColor = Leadwerks14Importer.ReadColor(reader);

            meshId = reader.ReadInt32();

            position = Leadwerks14Importer.ReadVector3(reader);
            rotation = Leadwerks14Importer.ReadVector3(reader);

            if ((flags & 1) == 0)
            {
                scale = Leadwerks14Importer.ReadVector3(reader);
            }

            int realIndex = content.indexMap[meshId];

            if (realIndex < content.MeshReferences.Count) meshRef = content.MeshReferences[realIndex];
            else 
                throw new InvalidContentException(string.Format("Mesh Reference {0} is invalid",meshId));

            for (int index = 0; index < meshRef.limbCount; index++)
            {
                reader.ReadInt32();
                if ((flags & 16) != 0)
                {
                    int vertexCount = reader.ReadInt16();
                    for (int vtx = 0; vtx < vertexCount; vtx++) Leadwerks14Importer.ReadColor(reader);
                }
            }
            
        }
    }
}